<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('@/echarts/customed') // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '350px',
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    xAxis: {
      type: Array,
    },
    chartData: {
      type: Array,
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        if (val) {
          this.setOptions(val)
        }
      },
    },
  },
  mounted() {
    this.$bus.$on('onresize', this.onResize)
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    this.$bus.$off('onresize', this.onResize)
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  activated() {
    this.onResize()
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'customed')
      this.setOptions(this.chartData)
    },
    setOptions(chartData) {
      if (!this.chart) {
        return
      }
      let option = {}
      if (!chartData || Object.keys(chartData).length === 0 || chartData.empty === true) {
        option = {
          xAxis: {
            data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            boundaryGap: false,
            axisTick: {
              show: false,
            },
          },
          grid: {
            left: 20,
            right: 20,
            bottom: 20,
            containLabel: true,
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
            },
            padding: [5, 10],
          },
          yAxis: {
            // 整条y轴
            axisLine: {
              show: true,
            },
            axisTick: {
              show: false,
            },
          },
          legend: {
            data: ['暂无数据'],
          },
          series: [
            {
              name: '暂无数据',
              smooth: true,
              type: 'line',
              data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              animationDuration: 2000,
              animationEasing: 'cubicInOut',
            },
          ],
        }
      } else {
        option = {
          tooltip: {},
          legend: {
            data: this.chartData.map((item) => item.name),
          },
          xAxis: {
            data: this.xAxis,
          },
          yAxis: {},
          series: this.chartData,
          grid: {
            left: 20,
            right: 20,
            bottom: 20,
            containLabel: true,
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
            },
            padding: [5, 10],
          },
          yAxis: {
            axisTick: {
              show: false,
            },
          },
        }
      }

      this.chart.setOption(option, true)
    },
    onResize() {
      if (this.chart) {
        this.chart.resize()
      }
    },
  },
}
</script>
